package com.wss.dao;

import com.wss.domain.Blog;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 博客dao接口
 * @author Renexton
 */
@Repository
public interface BlogDao {

    /**
     * 查询出所有博客
     * @return
     */
    @Select("select blog.*,btypename,username from blog inner join blogtype on (blog.btype_id = blogtype.id) inner join account on (account.id=blog.u_id) order by blog.id")
    List<Blog> findBlogAll();

    /**
     * 查询最新8条博客
     * @return
     */
    @Select("select blog.*,btypename,username from blog inner join blogtype on (blog.btype_id = blogtype.id) inner join account on (account.id=blog.u_id) order by blog.id desc limit 8")
    List<Blog> findBlogTop8();

    /**
     * 根据id查询博客
     * @param id
     * @return
     */
    @Select("select blog.*,username,btypename from blog inner join account on (blog.id = #{id} and blog.u_id = account.id) " +
            "inner join blogtype on (blogtype.id = blog.btype_id)")
    Blog findBlogById(int id);

    /**
     * 根据类型id查询博客
     * @param btype_id
     * @return
     */
    @Select("select blog.*,username,btypename from blog inner join account on (blog.btype_id = #{btype_id} and blog.u_id = account.id) " +
            "inner join blogtype on (blogtype.id = blog.btype_id) order by blog.id")
    List<Blog> findBlogByTypeId(int btype_id);

    /**
     * 根据用户id查询博客
     * @param u_id
     * @return
     */
    @Select("select blog.*,username,btypename from blog inner join account on (blog.u_id = #{u_id} and blog.u_id = account.id) " +
            "inner join blogtype on (blogtype.id = blog.btype_id) order by blog.id desc")
    List<Blog> findBlogByUid(int u_id);

    /**
     * 模糊查询博客标题，博客简介，博客内容，博客分类，用户名
     * @param input
     * @return
     */
    @Select("select blog.*,username,btypename from blog inner join account inner join blogtype on " +
            "(btitle like concat('%',#{input},'%') or bdesc like concat('%',#{input},'%') " +
            "or bcontent like concat('%',#{input},'%') or btypename like concat('%',#{input},'%') " +
            "or username like concat('%',#{input},'%')) " +
            "and (blog.u_id = account.id) and (blogtype.id = blog.btype_id) order by blog.id")
    List<Blog> findBlogByInputFromBlogTable(String input);

    /**
     * 模糊查询博客分类
     * @param input
     * @return
     */
    @Select("select blog.*,username from blog inner join blogtype on (btypename like concat('%',#{input},'%')) and (blogtype.id = blog.btype_id) inner join account on (blog.u_id = account.id) order by blog.id")
    List<Blog> findBlogByInputFromBType(String input);

    /**
     * 模糊查询博客作者名
     * @param input
     * @return
     */
    @Select("select blog.*,username from blog inner join account on (username like concat('%',#{input},'%')) and (blog.u_id = account.id) order by blog.id")
    List<Blog> findBlogByInputFromAccount(String input);

    /**
     * 添加博客
     * @param blog
     * @return
     */
    @Insert("insert into blog (btitle,bdesc,bcontent,btime,btype_id,u_id) values (#{btitle},#{bdesc},#{bcontent},curdate(),#{btype_id},#{u_id})")
    int addBlog(Blog blog);

    /**
     * 修改博客
     * @param blog
     * @return
     */
    @Update("update blog set btitle=#{btitle},bdesc=#{bdesc},bcontent=#{bcontent},btime=curdate(),btype_id=#{btype_id} where id=#{id}")
    int changeBlog(Blog blog);

    /**
     * 删除博客
     * 先删除博客表关联的评论表内容，最后再删除博客表数据
     * @param id
     * @return
     */
    @Delete("delete comments from blog inner join comments on (blog.id=comments.b_id) where blog.id=#{id}")
    int deleteCommentsWithBid(int id);
    @Delete("delete blog from blog where id=#{id}")
    int deleteBlog(int id);

}
